// PROGRAMA : IDCPE001
// BIBLIOTECA : PRODETR
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO
// PROGRAMADOR : EDU/FM DATA: 16/06/92
// OBJETIVO : EMITIR CARTEIRA DE IDENTIDADE. INFORMAR 'ECI'
// ATUALIZACOES DATA
// PROGRAMA : IDCPE001 24/10/1997
// BIBLIOTECA : SISP
// OBJETIVO : ADAPTACAO PARA O SHOPPING DO CIDADAO
// : DIVISAO DOS MODULOS EM 17/05/1999
// : ALTERACAO DE DATAS (N6) PARA (N8) EM 26/10/1999
// : INCLUSAO DO POSTO DO JOAO PAULO (P-002) EM 28/04/00
// : INCLUSAO DO POSTO DE IMPERATRIZ (P-005) EM 18/08/00
// OBS : USER-ID: IDCAG001 ATE IDCAG004(PARA ACAO GLOBAL)
// OBS1 : ESTE PROGRAMA FOI REESTRUTURADO POR SOLON MIRANDA
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;

import net.sf.jasperreports.engine.JRException;

import org.apache.commons.lang3.StringUtils;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.download.InputStreamDownload;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Idcta101Report;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Idcte101Report;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Idcte001;
import br.com.vti.sisp_sicrim.antigo.model.DidCadastro;
import br.com.vti.sisp_sicrim.antigo.model.DidEmissao;
import br.com.vti.sisp_sicrim.antigo.model.Estla001;
import br.com.vti.sisp_sicrim.antigo.model.SicControle;
import br.com.vti.sisp_sicrim.antigo.model.SicTabelas;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Idcpe001 OBJETIVO : EMITIR CARTEIRA DE IDENTIDADE. INFORMAR 'ECI' *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/idcpe001")
public class Idcpe001 {
	// Views utilizadas
	SicTabelas vwTabele = new SicTabelas();
	DidCadastro vwDidcad = new DidCadastro();
	SicControle vwDvecon = new SicControle();
	DidEmissao vwEmissao = new DidEmissao();
	DidCadastro didcad = new DidCadastro();
	// Telas/relatorios utilizados
	Idcte001 idcte001;
	Idcta101Report report;
	Idcte101Report report2;
	// Includes utilizados
	Estla001 estla001 = new Estla001();
	// Areas globais / session
	Sisgsisp sisgsisp;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Idcpe001 idcpe001Anterior;
	String faseproc = "0";

	// private String ctl = "";
	private int j = 0;
	private int i = 0;
	private int k = 0;
	private String conf = "";
	private String chassi = "";
	private String nommae = "";
	private String nompai = "";
	private String comuna = "";
	private int data8 = 0;
	private String[] numpos = new String[3];
	private String[] numvia = new String[3];
	private String[] pis = new String[3];
	private long[] pispas = new long[3];
	private String[] numcpf = new String[3];
	private String[] docum1 = new String[3];
	private String[] docum2 = new String[3];
	private int[] datn = new int[3];
	// private long[] regger = new long[3];
	private int[] datnas = new int[3];
	// private long[] ndemid = new long[3];
	private String[] comun = new String[3];
	// private int[] nvia = new int[3];
	private String[] ufnasc = new String[3];
	// 1 VVV (A2)
	// 1 REDEFINE VVV
	private String vvv = "";
	private int vv = 0;
	private String ppp = "";
	private String docume = "";
	private String nomide = "";
	private String nomid1 = "";
	private String nomid2 = "";
	private String filia1 = "";
	private String filia2 = "";
	private long[] numrg = new long[3];
	private long[] nserie = new long[3];
	private String[] nomeid = new String[3];
	private int horatemp = 0;
	private String jUser = UtilBean.getInitUser();
	private String tabDescel = "";
	// CABECALHO DO POSTO: POSTO
	// NUMERO DO POSTO
	// CONTEM
	private String chaveAlfa = "";
	// PROGRAMA: GETCHR
	// FUNCAO: CAPTURA O NOME LOGICO DO TERMINAL
	private int rTid = 0;
	private int len = 0;
	private int retcode = 0;
	private String getarea = "";
	private InputStreamDownload relatorio;

	private Idcte001 preMapas() {
		sessao.getScrg001().setWProgramaw(this.getClass().getSimpleName().toUpperCase());
		idcte001 = new Idcte001(true, sessao);
		idcte001.setDataCorrente(UtilBean.obtemDataDia());
		estla001.cpo = 5;
		estla001.esttip = 99;
		data8 = UtilBean.obtemDataCorrente();
		Arrays.fill(numpos, "");
		Arrays.fill(numvia, "");
		Arrays.fill(pis, "");
		Arrays.fill(pispas, 0);
		Arrays.fill(numcpf, "");
		Arrays.fill(docum1, "");
		Arrays.fill(docum2, "");
		Arrays.fill(datn, 0);
		Arrays.fill(datnas, 0);
		Arrays.fill(comun, "");
		Arrays.fill(ufnasc, "");
		Arrays.fill(numrg, 0);
		Arrays.fill(nserie, 0);
		Arrays.fill(nomeid, "");
		// SETA PF'S
		// Par�grafo: rp0()
		return idcte001;
	}

	public Idcpe001(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public InputStreamDownload executar(Idcte001 idcte001) {
		try {
			return main(idcte001);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroSiscerro(e, sessao, this.getClass().getSimpleName());
		}
		return relatorio;
	}

	private InputStreamDownload main(Idcte001 pidcte001) throws Exception {
		if (pidcte001 == null) {
			primeiraVez = true;
			// idcte001 = new Idcte001 (primeiraVez);
			idcte001 = preMapas();
			idcte001.setNavegar("../idcpe001/executar");
			result.include("idcte001", idcte001);
			sessao.setAtributo("IDCPE001", this);
			idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		} else {
			restaurarEstados(pidcte001);
		}

		Arrays.fill(numpos, "");
		Arrays.fill(numvia, "");
		Arrays.fill(pis, "");
		Arrays.fill(pispas, 0);
		Arrays.fill(numcpf, "");
		Arrays.fill(docum1, "");
		Arrays.fill(docum2, "");
		Arrays.fill(datn, 0);
		Arrays.fill(datnas, 0);
		Arrays.fill(comun, "");
		Arrays.fill(ufnasc, "");
		Arrays.fill(numrg, 0);
		Arrays.fill(nserie, 0);
		Arrays.fill(nomeid, "");
		report = new Idcta101Report();
		report2 = new Idcte101Report();

		switch (faseproc) {
		case "0":
			conf = "S";
			// DEFINE PRINTER(EFM = 1)
			idcte001.setCtl("");
			estla001.qde = 0;

			// ATUALIZAR A TABELA DE USER-ID A CADA NOVO USUARIO
			// TABELA DE USER-ID
			if (vwTabele.consultaTabcha(888, jUser)) {
				tabDescel = vwTabele.getDescricaoElem();
				idcte001.setP200(UtilBean.subStr(tabDescel, 0, 30));
				idcte001.setCab01(UtilBean.subStr(tabDescel, 30, 30));
			}
			vwTabele.fecha();
			if (vwTabele.registrosProcessados() == 0) {
				idcte001.setMensagem("USER-ID NAO AUTORIZADA PARA USAR O SISP:" + UtilBean.subStr(chaveAlfa, 3, 14));
				idcte001.setMensagem(idcte001.getMensagem() + "<br>FAVOR ENTRAR EM CONTATO COM O ANALISTA DO SISTEMA SISP");
				idcte001.setMensagem(idcte001.getMensagem() + "<br>ANALISTA DO SISP: SOLON");
				idcte001.setMensagem(idcte001.getMensagem() + "<br>TEL: (098) 227-5772 / 227-5599 / 227--5578 / 9974-8432");
				result.include("idcte001", idcte001);
				sessao.setAtributo("IDCPE001", this);
				idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return null;
			}

			idcte001.receberTela(false, result);
			if (idcte001.temMensagem) {
				result.include("idcte001", idcte001);
				sessao.setAtributo("IDCPE001", this);
				idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return null;
			}
			if (!idcte001.getOperacao().equals("ECI")) {
				// CHAMA PROGRAMA DE ACORDO COM A OPERACAO
				funcoes.sissm001(result, sessao);
				return null;
			}
			if (!UtilBean.funcao.equals(UtilBean.ctt_processa)) {
				idcte001.enviarMensagem(null, "");
				result.include("idcte001", idcte001);
				sessao.setAtributo("IDCPE001", this);
				idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return null;
			}
			funcoes.tabsemi(sessao);
			for (i = 1; i <= 2; i++) {
				didcad.consultaNumeroRg(idcte001.getRegger()[i]);
				idcte001.getNvia()[i] = didcad.getNumeroVia() + 1;
				didcad.fecha();
			}

			idcte001.setMensagemSaida("CONFIRME O NUMERO DE VIA");
			idcte001.setCtl("S");

			faseproc = "1";
			result.include("idcte001", idcte001);
			sessao.setAtributo("IDCPE001", this);
			// TELA DE EMISSAO DA CARTEIRA DE IDENTIDADE
			idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		case "1":
			idcte001.receberTela(false, result);
			if (idcte001.temMensagem) {
				result.include("idcte001", idcte001);
				sessao.setAtributo("IDCPE001", this);
				idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return null;
			}
			if (!idcte001.getOperacao().equals("ECI")) {
				// CHAMADA DE PROGRAMA DE ACORDO COM A OPERACAO
				funcoes.sissm001(result, sessao);
			}
			if (!UtilBean.funcao.equals(UtilBean.ctt_processa)) {
				idcte001.enviarMensagem(null, "");
				result.include("idcte001", idcte001);
				sessao.setAtributo("IDCPE001", this);
				idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return null;
			}

			for (i = 1; i <= 2; i++) {
				if (idcte001.getRegger()[i] == 0) {
					relatorio = imprime();
					break;
				}
				while (vwDvecon.pesquisaNumeroDocumentoTipoDocumentoIn(idcte001.getNdemid()[i], 87, 88)) {
					if (vwDvecon.getTipoDocumento() == 87) {
						vwDvecon.fecha();
						idcte001.enviarMensagem(null, "DOCUMENTO CANCELADO EMISSAO NAO PERMITIDA");
						result.include("idcte001", idcte001);
						sessao.setAtributo("IDCPE001", this);
						idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
						return null;
					}
					chassi = vwDvecon.getNumeroChassi();
					if (vwDvecon.getTipoDocumento() == 88 && Long.parseLong(UtilBean.subStr(chassi, 0, 13)) != idcte001.getRegger()[i]) {
						vwDvecon.fecha();
						idcte001.enviarMensagem(null, "DOCUMENTO EMITIDO COM RG:1:" + vwDvecon.getNumeroChassi());
						result.include("idcte001", idcte001);
						sessao.setAtributo("IDCPE001", this);
						idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
						return null;
					}
				}
				vwDvecon.fecha();
				nserie = idcte001.getNdemid();
				chassi = "";
				relatorio = acha();
			}
			funcoes.estna001(estla001.cpo, estla001.esttip, estla001.qde, sessao.getSisgsisp().getVar(), idcte001.getMatric());
			break;
		}

		// dao.rollback();
		faseproc = "0";
		if (relatorio != null) {
			return relatorio;
		} else {
			sessao.setAtributo("IDCPE001", this);
			result.include("idcte001", idcte001);
			if (!result.used()) {
				idcte001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			}
			return null;
		}
	}

	private InputStreamDownload acha() throws Exception {
		didcad.consultaNumeroRg(idcte001.getRegger()[i]);
		nomide = didcad.getNomeIdent();
		nompai = didcad.getNomePai();
		nommae = didcad.getNomeMae();
		numrg[i] = idcte001.getRegger()[i];
		nomeid[i] = didcad.getNomeIdent();
		ppp = UtilBean.moveSubstring(ppp, 0, 3, "N", didcad.getNumeroPosto());
		vvv = UtilBean.moveSubstring(vvv, 0, 2, "N", idcte001.getNvia()[i]);
		datn[i] = didcad.getDataDocumento();
		datn[i] = (int) funcoes.sissd002((long) datn[i]);
		// IF DINIDE-NUMVIA GT 0
		// IF DIAIDE-POLDIR = 'Y" || DIAIDE-INDDIR = 'Y" ||
		// DIAIDE-MEDDIR = 'Y" ||
		// DIAIDE-ANUDIR = 'Y" ||
		// DIAIDE-MINDIR = 'Y" ||
		// DIAIDE-POLESQ = 'Y" ||
		// DIAIDE-INDESQ = 'Y" ||
		// DIAIDE-MEDESQ = 'Y" ||
		// DIAIDE-ANUESQ = 'Y" ||
		// DIAIDE-MINESQ = 'Y'
		// PERFORM ROT-DATEMI
		// IF DATA8 GT (DATEMI + 600)
		// REINPUT 'EMISSAO NAO PERMITIDA P/DATILOSCOPIA VENCIDA'
		// ALARM
		// END-IF
		// END-IF
		numvia[i] = UtilBean.concatena("VIA-", vvv);
		// END-IF
		numpos[i] = UtilBean.concatena("P-", ppp);
		if (nommae.trim().equals("")) {
			nommae = "************";
		}
		if (nompai.trim().equals("")) {
			nompai = "************";
		}
		if (didcad.getNacionalidadeIdent() == 1) {
			vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 0, 3, "N", 1);
			vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 3, 12, "N", didcad.getNomeMunicipioIdent());
			vwTabele.resp = funcoes.sisst001(1, didcad.getNomeMunicipioIdent(), vwTabele.resp);
			comuna = vwTabele.resp;
			if (UtilBean.subStr(vwTabele.resp, 0, 1).equals("*")) {
				vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 0, 3, "N", 209);
				vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 3, 12, "N", didcad.getPaisOrigem());
				vwTabele.resp = funcoes.sisst001(209, didcad.getPaisOrigem(), vwTabele.resp);
				comuna = vwTabele.resp;
			}

			comun[i] = comuna;
			comun[i] = UtilBean.concatena(comuna, "-", didcad.getUfMunicipioNasc());
		} else {
			vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 0, 3, "N", 209);
			vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 3, 12, "N", didcad.getPaisOrigem());
			vwTabele.resp = funcoes.sisst001(209, didcad.getPaisOrigem(), vwTabele.resp);
			comuna = vwTabele.resp;

			comun[i] = comuna;
		}
		switch (didcad.getTipoDocumento()) {
		case 1: {
			docume = UtilBean.concatena("NASC.N.", didcad.getNumeroCertidao(), " FLS.", didcad.getNumeroFolha(), " LIV.", didcad.getNumeroLivro());
			break;
		}
		case 2: {
			docume = UtilBean.concatena("CASAM.N.", didcad.getNumeroCertidao(), " FLS.", didcad.getNumeroFolha(), " LIV.", didcad.getNumeroLivro());
			break;
		}
		case 3: {
			docume = UtilBean.concatena("PORT.MINISTERIAL-N.", didcad.getNumeroPortariaMinist(), "/", didcad.getAnoPortariaMinist());
			vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 0, 3, "N", 209);
			vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 3, 12, "N", didcad.getPaisOrigem());
			vwTabele.resp = funcoes.sisst001(209, didcad.getPaisOrigem(), vwTabele.resp);
			// PESQUISA TABELA

			comuna = vwTabele.resp;
			break;
		}
		case 4: {
			docume = UtilBean.moveSubstring(docume, 0, 35, "A", "NAC. PORTUGUESA-PORT. MINISTERIAL");
			docume = UtilBean.moveSubstring(docume, 35, 20, "A", UtilBean.concatena("N.", didcad.getNumeroPortariaMinist(), "/", didcad.getAnoPortariaMinist()));
			vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 0, 3, "N", 209);
			vwTabele.pesq = UtilBean.moveSubstring(vwTabele.pesq, 3, 12, "N", didcad.getPaisOrigem());
			vwTabele.resp = funcoes.sisst001(209, didcad.getPaisOrigem(), vwTabele.resp);
			// PESQUISA TABELA

			comuna = vwTabele.resp;
			break;
		}
		case 5: {
			docume = UtilBean.concatena("SEP.JUD.-N.", didcad.getNumeroCertidao(), " FLS.", didcad.getNumeroFolha(), " LIV.", didcad.getNumeroLivro());
			break;
		}
		case 6: {
			docume = UtilBean.concatena("SEP.DIV.-N.", didcad.getNumeroCertidao(), " FLS.", didcad.getNumeroFolha(), " LIV.", didcad.getNumeroLivro());
			break;
		}
		default: {
			break;
		}
		}

		docum1[i] = UtilBean.subStr(docume, 0, 35);

		docum2[i] = UtilBean.subStr(docume, 35, 20);
		docume = "";
		for (j = 1; j <= 37; j++) {
			if (UtilBean.subStr(nomide, j, 1).trim().equals("")) {
				if (UtilBean.subStr(nomide, j + 1, 1).trim().equals("")) {
					break;
				}
			}
		}
		switch (i) {
		case 1: {
			filia1 = String.format("%35s%35s%35s", nompai, "E", nommae);
			if (j < 36) {

				nomid1 = UtilBean.moveSubstring(nomid1, 35, 35, "A", nomide);
			} else {

				nomid1 = nomide;
			}
			break;
		}
		case 2: {
			filia2 = String.format("%35s%35s%35s", nompai, "E", nommae);
			if (j < 36) {

				nomid2 = UtilBean.moveSubstring(nomid2, 35, 35, "A", nomide);
			} else {

				nomid2 = nomide;
			}
			break;
		}
		default: {
			break;
		}
		}

		nomide = "";
		nompai = "";
		nommae = "";
		datnas[i] = didcad.getDataNasc();
		datnas[i] = (int) funcoes.sissd002(datnas[i]);
		if (didcad.getRegistroAnterior() > 0 && didcad.getUfRegAnterior() == sessao.getSisgsisp().getUfloca()) {
			pis[i] = "RG. ANT.";
			pispas[i] = didcad.getRegistroAnterior();
		} else {
			if (didcad.getNumeroPisPasep() > 0) {

				pispas[i] = didcad.getNumeroPisPasep();
				pis[i] = "PIS/PASEP";
			} else {
				pispas[i] = 0;
				pis[i] = "";
			}
		}
		if (didcad.getNumeroCpf() > 0) {
			numcpf[i] = UtilBean.moveSubstring("", 0, 11, "N", didcad.getNumeroCpf());
		} else {
			numcpf[i] = StringUtils.repeat("*", 11);
		}
		if (numcpf[i].equals("0")) {
			numcpf[i] = StringUtils.repeat("*", 11);
		}

		didcad.setNumeroVia(Integer.parseInt(vvv));
		chassi = UtilBean.moveSubstring("", 0, 13, "N", idcte001.getRegger()[i]);
		didcad.atualiza();
		// dao.commit();
		vwDvecon.setNumeroChassi(chassi);
		vwDvecon.setNumeroDocumento(idcte001.getNdemid()[i]);
		vwDvecon.setDataEmissao(data8);
		vwDvecon.setTipoDocumento(88);
		vwDvecon.setMatriculaFunc(sessao.getSisgsisp().getMatric());
		vwDvecon.setCodigoMunicipio(Integer.parseInt(UtilBean.subStr(ppp, 0, 3)));
		vwDvecon.setCodigoRecuperacao(Integer.parseInt(vvv));
		vwDvecon.grava();
		// dao.commit();
		estla001.qde = i;

		report.setRegger(1, idcte001.getRegger()[1]);
		report.setRegger(2, idcte001.getRegger()[2]);
		report.setDatn(1, datn[1]);
		report.setDatn(2, datn[2]);
		report.setNom1(UtilBean.subStr(nomid1, 0, 35));
		report.setNom2(UtilBean.subStr(nomid1, 35, 35));
		report.setNom3(UtilBean.subStr(nomid2, 0, 35));
		report.setNom4(UtilBean.subStr(nomid2, 35, 35));
		report.setF1(UtilBean.subStr(filia1, 0, 35));
		report.setF2(UtilBean.subStr(filia1, 35, 35));
		report.setF3(UtilBean.subStr(filia1, 70, 35));
		report.setF4(UtilBean.subStr(filia2, 0, 35));
		report.setF5(UtilBean.subStr(filia2, 35, 35));
		report.setF6(UtilBean.subStr(filia2, 70, 35));
		report.setComun(1, comun[1]);
		report.setComun(2, comun[2]);
		report.setDatnas(1, datnas[1]);
		report.setDatnas(2, datnas[2]);
		report.setDocum1(1, docum1[1]);
		report.setDocum1(2, docum1[2]);
		report.setDocum2(1, docum2[1]);
		report.setDocum2(2, docum2[2]);
		report.setPis(1, pis[1]);
		report.setPis(2, pis[2]);
		report.setNumcpf(1, numcpf[1]);
		report.setNumcpf(2, numcpf[2]);
		report.setPispas(1, pispas[1]);
		report.setPispas(2, pispas[2]);
		report.setNumpos(1, numpos[1]);
		report.setNumpos(2, numpos[2]);
		report.setNumvia(1, numvia[1]);
		report.setNumvia(2, numvia[2]);

		report2.setRegger(1, idcte001.getRegger()[1]);
		report2.setRegger(2, idcte001.getRegger()[2]);
		report2.setDatn(1, datn[1]);
		report2.setDatn(2, datn[2]);
		report2.setNom1(UtilBean.subStr(nomid1, 0, 35));
		report2.setNom2(UtilBean.subStr(nomid1, 35, 35));
		report2.setNom3(UtilBean.subStr(nomid2, 0, 35));
		report2.setNom4(UtilBean.subStr(nomid2, 35, 35));
		report2.setF1(UtilBean.subStr(filia1, 0, 35));
		report2.setF2(UtilBean.subStr(filia1, 35, 35));
		report2.setF3(UtilBean.subStr(filia1, 70, 35));
		report2.setF4(UtilBean.subStr(filia2, 0, 35));
		report2.setF5(UtilBean.subStr(filia2, 35, 35));
		report2.setF6(UtilBean.subStr(filia2, 70, 35));
		report2.setComun(1, comun[1]);
		report2.setComun(2, comun[2]);
		report2.setDatnas(1, datnas[1]);
		report2.setDatnas(2, datnas[2]);
		report2.setDocum1(1, docum1[1]);
		report2.setDocum1(2, docum1[2]);
		report2.setDocum2(1, docum2[1]);
		report2.setDocum2(2, docum2[2]);
		report2.setPis(1, pis[1]);
		report2.setPis(2, pis[2]);
		report2.setNumcpf(1, numcpf[1]);
		report2.setNumcpf(2, numcpf[2]);
		report2.setPispas(1, pispas[1]);
		report2.setPispas(2, pispas[2]);
		report2.setNumpos(1, numpos[1]);
		report2.setNumpos(2, numpos[2]);
		report2.setNumvia(1, numvia[1]);
		report2.setNumvia(2, numvia[2]);

		if (i == 2) {
			return imprime();
		}
		if (Integer.parseInt(UtilBean.subStr(jUser, 6, 2)) < 8) {
			for (k = 1; k <= 2; k++) {
				vwEmissao.setRegGeralEmitido(numrg[k]);
				vwEmissao.setTipReg(1);
				vwEmissao.setNomeIdent(nomeid[k]);
				vwEmissao.setNumeroSerieRg(nserie[k]);
				vwEmissao.setDataEmissao(UtilBean.obtemDataCorrente());
				horatemp = Integer.parseInt((new SimpleDateFormat("HHmmss").format(Calendar.getInstance().getTime())));
				vwEmissao.setHoraEmissaoCarteira(horatemp);
				vwEmissao.setMatriculaFunc(sessao.getSisgsisp().getMatric());
				vwEmissao.grava();
				// dao.commit();
			}
		}
		return null;

	}

	private InputStreamDownload imprime() throws JRException, ParseException, FileNotFoundException {
		idcte001.setMensagemSaida("EMISSAO EFETUADA OK");
		// RELATORIO
		// MP = 1;
		if (idcte001.getRegger()[i] == 173624920014L) {
			datnas[i] = 29021929;
		}
		if (vwDidcad.getDataNasc() == 19290228) {
			datnas[i] = 29021929;
		}
		// OSA
		len = 148;
		retcode = funcoes.getchr(getarea, len, rTid);
		if (UtilBean.subStr(getarea, 56, 8).equals("TVCPG001") || UtilBean.subStr(getarea, 56, 8).equals("TVCPG002") || UtilBean.subStr(getarea, 56, 8).equals("TVCPG003") || UtilBean.subStr(getarea, 56, 8).equals("TVCPG004") || UtilBean.subStr(getarea, 56, 8).equals("TVCPG005") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV111") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV112") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV131") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV051") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV052") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV061") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV062") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV071") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV072") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV081") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV082") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV091") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV092") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV101") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV102") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV111") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV112") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV121") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV122") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV131") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV132") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV141") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV151") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV152") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV161") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV162") || UtilBean.subStr(getarea, 56, 8).equals("TVCCL001") || UtilBean.subStr(getarea, 56, 8).equals("TVCCL002") || UtilBean.subStr(getarea, 56, 8).equals("TVCCL003") || UtilBean.subStr(getarea, 56, 8).equals("TVCJP004") || UtilBean.subStr(getarea, 56, 8).equals("TVCJP001") || UtilBean.subStr(getarea, 56, 8).equals("TVCJP002") || UtilBean.subStr(getarea, 56, 8).equals("TVCJP003") || UtilBean.subStr(getarea, 56, 8).equals("TVCJP005") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV132") || UtilBean.subStr(getarea, 56, 8).equals("TSTI0003") || UtilBean.subStr(getarea, 56, 8).equals("TVCMV152") || UtilBean.subStr(getarea, 56, 8).equals("TVCIPZ01") || UtilBean.subStr(getarea, 56, 8).equals("TVCIPZ02") || UtilBean.subStr(getarea, 56, 8).equals("TVCIPZ03") || UtilBean.subStr(getarea, 56, 8).equals("TVCIPZ04") || UtilBean.subStr(getarea, 56, 8).equals("TVCIPZ05")) {
			report.gravaRelatorio(1);
			return report.imprimir();
			// RELATORIO
		} else {
			report2.gravaRelatorio(1);
			return report2.imprimir();
			// RELATORIO
		}
		// if (UtilBean.subStr(getarea, 56, 8).equals("TVCMV142")) {
		// // DEFINE PRINTER(2)
		// // RELATORIO
		// // MP = 1;
		// report.gravaRelatorio(1);
		// report.imprimir();
		// // RELATORIO
		// }
	}

	private void restaurarEstados(Idcte001 pidcte001) {
		this.idcpe001Anterior = (Idcpe001) sessao.getAtributo("IDCPE001");
		if (pidcte001 != null) {
			idcte001 = pidcte001;
			idcte001.setMatric(sessao.getSisgsisp().getMatric());
			idcte001.setOperacao(sessao.getSisgsisp().getOperacao());
			idcte001.setSenha(sessao.getSisgsisp().getSenha());
			idcte001.setJversaoj(sessao.getScrg001().getWVersaow());
			idcte001.setJempresaj(sessao.getScrg001().getWEmpresaw());
			idcte001.setNomeClasse(sessao.getScrg001().getWProgramaw());
			idcte001.setJfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			idcte001 = this.idcpe001Anterior.idcte001;
		}
		idcte001.setSessao(sessao);
		this.faseproc = this.idcpe001Anterior.faseproc;
	}
}
